package com.bitcat.todoapp.demo.tablayoutdemo

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.bitcat.todoapp.R
import com.bitcat.todoapp.databinding.ActivityTabLayoutDemoBinding
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.google.android.material.tabs.TabLayoutMediator
import es.dmoral.toasty.Toasty

class TabLayoutDemoActivity : AppCompatActivity() {

    private val binding: ActivityTabLayoutDemoBinding by lazy {
        ActivityTabLayoutDemoBinding.inflate(layoutInflater)
    }

    private val titleList = listOf("关注", "推荐", "发现", "热榜", "凡人修仙传","生活")

    private val iconList = listOf(
        R.drawable.home,
        R.drawable.news,
        R.drawable.news,
        R.drawable.home,
        R.drawable.news,
        R.drawable.news,
    )



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        titleList.forEachIndexed { index, title ->
            val tab = binding.tabLayout2.newTab()
            tab.text = title
            tab.setIcon(iconList[index])
            binding.tabLayout2.addTab(tab)
        }
        



        //添加tab
//        for (title in titleList) {
//            val tab = binding.tabLayout.newTab()
//            tab.text = title
//            binding.tabLayout.addTab(tab)
//        }

        titleList.forEach {
            title -> binding.tabLayout.addTab(binding.tabLayout.newTab().setText(title))
        }

        binding.tabLayout.addOnTabSelectedListener(object:OnTabSelectedListener{
            override fun onTabSelected(tab: TabLayout.Tab) {
                Toasty.info(this@TabLayoutDemoActivity,tab.text.toString()).show()
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }
        })


        //viewPager2 + TabLayout
        binding.viewPager.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int = titleList.size

            override fun createFragment(position: Int): Fragment = TabContentFragment.newInstance(titleList[position])

        }
        //联动
//        TabLayoutMediator(binding.tabLayout,binding.viewPager,object :
//            TabLayoutMediator.TabConfigurationStrategy{
//            override fun onConfigureTab(tab: TabLayout.Tab, position: Int) {
//                TODO("Not yet implemented")
//            }
//        })

        TabLayoutMediator(binding.tabLayout,binding.viewPager
        ) { tab, position ->
            tab.text = titleList[position]
        }.attach()


    }
}